WASHINGTON — Control of Congress and the future of Donald Trump’s presidency are on the line as the primary season closes this week, jump-starting a two-month sprint to Election Day that will test Democrats’ ability to harness opposition to Trump and determine whether the Republican president can get his supporters to the polls.

For both parties, the stakes are exceedingly high.

After crushing defeats in 2016, Democrats open the fall campaign brimming with confidence about their prospects for retaking the House, which would give them power to open a wide swath of investigations into Trump or even launch impeachment proceedings. The outcome of the election, which features a record number of Democratic female and minority candidates, will also help shape the party’s direction heading into the 2020 presidential race.

Republicans have spent the primary season anxiously watching suburban voters, particularly women, peel away because of their disdain for Trump. The shift seems likely to cost the party in several key congressional races. Still, party leaders are optimistic that Republicans can keep control of the Senate, which could help insulate Trump from a raft of Democratic investigations.

History is not on Trump’s side. The president’s party typically suffers big losses in the first midterm election after taking office. And despite a strong economy, Republicans must also contend with the president’s sagging approval rating and the constant swirl of controversy hanging over the White House, including special counsel Robert Mueller’s ongoing probe into Russian election interference and possible obstruction of justice by Trump.

Despite those headwinds, Trump is betting on himself this fall. He’s thrust himself into the center of the campaign and believes he can ramp up turnout among his ardent supporters and offset a wave of Democratic enthusiasm. Aides say he’ll spend much of the fall holding rallies in swing states.

Despite Democrats’ optimism heading into the fall, party officials concede that taking back control of the Senate may not be realistic.

“The great unknown is whether the president can mobilize his base to meet the enthusiasm gap that is clearly presented at this point,” said Josh Holmes, a longtime adviser to Senate Majority Leader Mitch McConnell. “Because the middle won’t be there for Republicans.”

Indeed, Trump’s turbulent summer appears to have put many moderates and independents out of reach for Republican candidates, according to GOP officials. One internal GOP poll obtained by The Associated Press showed Trump’s approval rating among independents in congressional battleground districts dropped 10 points between June and August.

A GOP official who oversaw the survey attributed the drop to negative views of Trump’s meeting with Russian President Vladimir Putin and the White House’s policy of separating immigrant children from their parents at the U.S.-Mexico border. The official was not authorized to discuss the internal polling publicly and spoke on condition of anonymity.

Those declines put several incumbent GOP lawmakers at risk, including Virginia Rep. Barbara Comstock, who represents a district in the Washington suburbs, and Rep. Erik Paulsen, whose suburban Minneapolis district has been in Republican hands since 1961.

Democrats need a net gain of 23 seats to take control of the House. Operatives in both parties believe at least 40 seats will be competitive in November.

Corry Bliss, who runs a super PAC aligned with House Speaker Paul Ryan, acknowledged a “tough environment” for Republicans that could quickly become too difficult for some incumbents to overcome.

“Incumbents who wake up down in the beginning of October are not going to be able to fix it in this environment,” Bliss said. “But incumbents who go on the offense early can and will win.”

Democratic incumbents had a similar wakeup call during the primaries after New York Rep. Joe Crowley, who held a powerful leadership position in Congress, stunningly lost to 28-year-old first-time candidate Alexandria Ocasio-Cortez. She’s among several younger minority candidates who defeated older, more established opponents, signaling a desire among many Democratic voters for generational change.

The result is a Democratic field with more women and minorities on the general-election ballot than ever before, several of whom are poised to make history if elected. Ayanna Pressley, who defeated 10-term Rep. Michael Capuano in a primary last week and is unopposed in the general election, will be the first black woman to represent Massachusetts in Congress. Rashida Talib of Michigan is on track to become the first Muslim woman in Congress. And Stacey Abrams in Georgia and Andrew Gillum in Florida would be their states’ first black governors if elected this fall.

Crowley said the wave that led to his own defeat will have long-term benefits for the Democratic Party if it motivates more young people and minorities to vote.

“Look at the positives for the country in terms of engagement and the activity that it’s causing and fervor that is forming,” Crowley said.

Indeed, turnout for Democrats has been high in a series of special elections that preceded the November contest. Nearly 60 Democratic challengers outraised House Republicans in the second quarter of 2018. And of the 10 Senate Democrats running for re-election in states Trump carried two years ago, only Florida Sen. Bill Nelson has been outraised by his Republican opponent.

“We’ve got real wind at our back,” said Tom Perez, the chairman of the Democratic National Committee. “The breadth and depth of the map is remarkable.”

Despite Democrats’ optimism heading into the fall, party officials concede that taking back control of the Senate may not be realistic. Unlike the competitive House races, which are being fought in territory that is increasingly favorable to Democrats, the most competitive Senate contests are in states Trump won — often decisively.

Democratic operatives are increasingly worried about Sen. Heidi Heitkamp’s ability to hang on in North Dakota, a state Trump won by 36 points and visited on Friday. Democratic incumbents also face more conservative electorates in Missouri, Indiana and Montana.

Still, Democrats believe that if momentum builds through the fall and Trump’s approval rating sinks further, the party could not only hold onto its current Senate seats but also add wins in territory that has long been out of reach, including Tennessee and Texas, where Rep. Beto O’Rourke is giving Republican Sen. Ted Cruz a surprising re-election fight.

“There’s engagement and momentum like I haven’t seen since the Ann Richards days,” said Texas Democratic Chairman Gilberto Hinojosa, referring to the state’s Democratic governor in the early 1990s.

While most of the attention is on the battle for Congress, competition for the Democratic presidential nomination in 2020 is heating up. New Jersey Sen. Cory Booker is scheduled to headline the marquee fall banquet for Iowa Democrats next month.

For now, former President Barack Obama is emerging as the top Democrat making the case for the party this fall. He returned to the political fray last week imploring voters upset with Trump to show up in November.

“Just a glance at recent headlines should tell you this moment really is different,” Obama said in a speech Friday. “The stakes really are higher. The consequences of any of us sitting on the sidelines are more dire.”

Associated Press writers Bill Barrow and Kevin Freking contributed to this report.

READ MORE: Female candidates are talking more about sexual misconduct. Will voters care?